<?php

class Core_AdminLayoutController extends Zend_Controller_Action
{
	public function indexAction()
	{
		$table = Nuo::getTable('core.layout');

		$select = $table -> select();

		$pag = $this -> view -> paginator = Zend_Paginator::factory($select);

		$pag -> setCurrentPageNumber($this -> _getParam('page', 1));

	}

	public function blocksAction()
	{
		$table = Nuo::getTable('core.layout.block');

		$select = $table -> select();

		$id = $this -> _getParam('layout_id', 1);

		$select -> where('layout_id=?', $id);

		$this -> view -> items = $table -> fetchAll($select);
	}

	public function editAction()
	{
		/**
		 * @var Zend_Form
		 */
		$form = $this -> view -> form = new Core_Form_Admin_Layout_Edit;

		/**
		 * @var Request
		 */
		$req = $this -> getRequest();

		/**
		 * @var Nuo_Db_Table
		 */
		$table = Nuo::getTable('core.layout');

		/**
		 * @var int
		 */
		$id = $this -> _getParam('layout_id', 0);

		/**
		 * @var Nuo_Db_Table_Row
		 */
		$item = null;

		if ($id)
		{
			$item = $table -> find($id) -> current();
		}

		/**
		 * is GET
		 */
		if ($req -> isGet())
		{
			if (is_object($item))
			{
				$form -> populate($item -> toArray());
			}
		}

		/**
		 * is POST
		 */
		if ($req -> isPost() && $form -> isValid($req -> getPost()))
		{
			if (!is_object($item))
			{
				$item = $table -> fetchNew();
			}

			$item -> setFromArray($form -> getValues());

			$item -> save();

			$url = Zend_Controller_Front::getInstance() -> getRouter() -> assemble(array(
					'module' => 'core',
					'controller' => 'layout',
					'action' => 'index'
			), 'admin', true);

			$this -> _helper -> redirector -> gotoUrl($url, array('prependBase' => 0));
		}

	}

	public function addAction()
	{
		/**
		 * @var Zend_Form
		 */
		$form = $this -> view -> form = new Core_Form_Admin_Layout_Add;

		/**
		 * @var Request
		 */
		$req = $this -> getRequest();

		/**
		 * @var Nuo_Db_Table
		 */
		$table = Nuo::getTable('core.layout');

		/**
		 * @var int
		 */
		$id = $this -> _getParam('layout_id', 0);

		/**
		 * @var Nuo_Db_Table_Row
		 */
		$item = null;

		if ($id)
		{
			$item = $table -> find($id) -> current();
		}

		/**
		 * is GET
		 */
		if ($req -> isGet())
		{
			if (is_object($item))
			{
				$form -> populate($item -> toArray());
			}
		}

		/**
		 * is POST
		 */
		if ($req -> isPost() && $form -> isValid($req -> getPost()))
		{
			$item = $table -> fetchNew();

			$item -> setFromArray($form -> getValues());

			$item -> save();

			$url = Zend_Controller_Front::getInstance() -> getRouter() -> assemble(array(
					'module' => 'core',
					'controller' => 'layout',
					'action' => 'index'
			), 'admin', true);

			$this -> _helper -> redirector -> gotoUrl($url, array('prependBase' => 0));
		}

	}

	public function deleteAction()
	{
		/**
		 * @var Zend_Form
		 */
		$form = $this -> view -> form = new Core_Form_Admin_Layout_Delete;

		/**
		 * @var Request
		 */
		$req = $this -> getRequest();

		/**
		 * @var Nuo_Db_Table
		 */
		$table = Nuo::getTable('core.layout');

		/**
		 * @var int
		 */
		$id = $this -> _getParam('layout_id', 0);

		/**
		 * @var Nuo_Db_Table_Row
		 */
		$item = null;

		if ($id)
		{
			$item = $table -> find($id) -> current();
		}

		/**
		 * is GET
		 */
		if ($req -> isGet())
		{
			if (is_object($item))
			{
				$form -> populate($item -> toArray());
			}
		}

		/**
		 * is POST
		 */
		if ($req -> isPost() && $form -> isValid($req -> getPost()))
		{

			if (is_object($item))
			{
				$item -> delete();
			}

			$url = Zend_Controller_Front::getInstance() -> getRouter() -> assemble(array(
					'module' => 'core',
					'controller' => 'layout',
					'action' => 'index'
			), 'admin', true);

			$this -> _helper -> redirector -> gotoUrl($url, array('prependBase' => 0));
		}
	}

	/**
	 * add block
	 */
	public function blockAddAction()
	{
		/**
		 * @var Zend_Form
		 */
		$form = $this -> view -> form = new Core_Form_Admin_Layout_Block_Add;

		/**
		 * @var Request
		 */
		$req = $this -> getRequest();

		/**
		 * @var Nuo_Db_Table
		 */
		$table = Nuo::getTable('core.layout');

		/**
		 * @var int
		 */
		$id = $this -> _getParam('layout_id', 0);

		/**
		 * @var Nuo_Db_Table_Row
		 */
		$item = null;

		if ($id)
		{
			$item = $table -> find($id) -> current();
		}

		/**
		 * @var Block
		 */
		$block = null;
		
		/**
		 * @var Core_Model_DbTable_Block
		 */
		$tableBlock = Nuo::getTable('core.layout.block');

		/**
		 * @var int
		 */
		$block_id = $this -> _getParam('layout_id', 0);

		if ($block_id)
		{
			$block = $tableBlock -> find($block_id) -> current();
		}

		/**
		 * is GET
		 */
		if ($req -> isGet())
		{
			if (is_object($block))
			{
				$form -> populate($block -> toArray());
			}
		}

		/**
		 * is POST
		 */
		if ($req -> isPost() && $form -> isValid($req -> getPost()))
		{
			$block = $tableBlock -> fetchNew();

			$block -> setFromArray($form -> getValues());

			$block -> save();

			$url = Zend_Controller_Front::getInstance() -> getRouter() -> assemble(array(
					'module' => 'core',
					'controller' => 'blocks',
					'action' => 'index'
			), 'admin', true);

			$this -> _helper -> redirector -> gotoUrl($url, array('prependBase' => 0));
		}

	}

}
